/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.sys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.apestar.common.DataContants;
import cn.apestar.common.beanvalidator.BeanValidators;
import cn.apestar.common.service.TreeService;
import cn.apestar.common.utils.SpringContextHolder;
import cn.apestar.common.utils.tran.TransLateCondition;
import cn.apestar.common.utils.tran.TranslateUtil;
import cn.apestar.modules.sys.dao.AreaDao;
import cn.apestar.modules.sys.entity.Area;
import cn.apestar.modules.sys.entity.AreaExcel;
import cn.apestar.modules.sys.entity.AreaRequest;
import cn.apestar.modules.sys.entity.User;
import cn.apestar.modules.sys.utils.UserUtils;

/**
 * 区域Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class AreaService extends TreeService<AreaDao, Area> {

    /**
     * 验证Bean实例对象
     */
    @Autowired
    protected Validator validator;

    @Resource
    private AreaDao areaDao;

    public List<Area> findAll() {

        return dao.findAllList(new Area());
    }

    public List<Area> findCnList(Area area) {

        return areaDao.findCnList(area);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(Area area) {

        if (area.getParent() != null)
            area.setParentId(area.getParent().getId());
        super.save(area);
        UserUtils.removeCache(UserUtils.CACHE_AREA_LIST);
    }

    @Transactional(readOnly = false)
    public void updateType(String id,String status) {
        areaDao.updateType(id,status);
        UserUtils.removeCache(UserUtils.CACHE_AREA_LIST);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(Area area) {

        super.delete(area);
        UserUtils.removeCache(UserUtils.CACHE_AREA_LIST);
    }

    /**
     * 通过名称模糊查询对应的区域.
     * @param name 区域的名称，例如文灶
     * @return
     */
    public List<Area> getAreas(String name) {

        return areaDao.findAreas(name);
    }

    public List getP(String id) {

        return dao.getP(id);
    }

    public List findlist() {

        return dao.findAllList();
    }

    @Transactional(readOnly = false)
    public Map saveSheng(AreaExcel areaExcel, Map map) {

        Area area = new Area();
        if (areaExcel.getGjName() != null && map.containsKey(areaExcel.getGjName())) {
            Area mapArea = (Area) map.get(areaExcel.getGjName());
            area.setParentIds(mapArea.getParentIds() + areaExcel.getGjCode() + ",");
            area.setParentId(mapArea.getId());
        }
        area.setCreateBy(new User("1"));
        area.setUpdateBy(new User("1"));
        area.setUpdateDate(new Date());
        area.setCode(areaExcel.getShengCode());
        area.setName(areaExcel.getShengName());
        area.setCreateDate(new Date());
        area.setId(areaExcel.getShengCode());
        area.setParent(new Area(area.getParentId()));
        area.setType(DataContants.AREA_SHENG);
        beanValidatorForApp(area);
        insertOnly(area);
        map.put(area.getName(), area);
        return map;
    }

    @Transactional(readOnly = false)
    public Map saveCity(AreaExcel areaExcel, Map map) {

        Area area = new Area();
        if (areaExcel.getGjName() != null && map.containsKey(areaExcel.getShengName())) {
            Area mapArea = (Area) map.get(areaExcel.getShengName());
            area.setParentIds(mapArea.getParentIds() + areaExcel.getShengCode() + ",");
            area.setParentId(mapArea.getId());
        }
        area.setCreateBy(new User("1"));
        area.setUpdateBy(new User("1"));
        area.setUpdateDate(new Date());
        area.setCode(areaExcel.getCityCode());
        area.setName(areaExcel.getCityName());
        area.setId(areaExcel.getCityCode());
        area.setCreateDate(new Date());
        area.setParent(new Area(area.getParentId()));
        area.setType(DataContants.AREA_SHI);
        beanValidatorForApp(area);
        insertOnly(area);
        map.put(area.getName(), area);
        return map;
    }

    @Transactional(readOnly = false)
    public Map saveXian(AreaExcel areaExcel, Map map) {

        Area area = new Area();
        if (areaExcel.getGjName() != null && map.containsKey(areaExcel.getCityName())) {
            Area mapArea = (Area) map.get(areaExcel.getCityName());
            area.setParentIds(mapArea.getParentIds() + areaExcel.getCityCode() + ",");
            area.setParentId(mapArea.getId());
        }
        area.setCreateBy(new User("1"));
        area.setUpdateBy(new User("1"));
        area.setUpdateDate(new Date());
        area.setId(areaExcel.getQuCode());
        area.setCode(areaExcel.getQuCode());
        area.setName(areaExcel.getQuName());
        area.setCreateDate(new Date());
        area.setParent(new Area(area.getParentId()));
        area.setType(DataContants.AREA_XIAN);
        beanValidatorForApp(area);
        insertOnly(area);
        map.put(area.getName(), area);
        return map;
    }

    protected List<String> beanValidatorForApp(Object object, Class<?>... groups) {

        try {
            BeanValidators.validateWithException(validator, object, groups);
        } catch (ConstraintViolationException ex) {
            return BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
        }
        return new ArrayList<String>();
    }

    /**
     * 取的商圈信息
     * @return
     */
    public List<Area> getShangQuanList(String name) {

        AreaDao areaDao = (AreaDao) SpringContextHolder.getBean("areaDao");
        Area area = new Area();
        area.setParentId(null);
        area.setName(name);
        List areaList = areaDao.getShangQuanList(area);
        TranslateUtil.t.translateList(areaList, new TransLateCondition("parentId", "sys_area a", "a.name as areaPname", "id", null), true);
        return areaList;
    }

    /**
     * 保存数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public void updateLatLng(Area area) {
        dao.updateLatLng(area);
    }

    public List<Area> getFetchList() {

        return dao.getFetchList();
    }

    public List<Area> getFetchListHave(Area area) {

        return dao.getFetchListHave(area);
    }

    public List<Area> getAreaByIds(AreaRequest ids) {

        return dao.getAreaByIds(ids);
    }
}
